﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpanHelper2
{
    public partial class Form1 : Form
    {
        //定义常量  
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_CONFIGCHANGECANCELED = 0x0019;
        public const int DBT_CONFIGCHANGED = 0x0018;
        public const int DBT_CUSTOMEVENT = 0x8006;
        public const int DBT_DEVICEQUERYREMOVE = 0x8001;
        public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public const int DBT_DEVICEREMOVEPENDING = 0x8003;
        public const int DBT_DEVICETYPESPECIFIC = 0x8005;
        public const int DBT_DEVNODES_CHANGED = 0x0007;
        public const int DBT_QUERYCHANGECONFIG = 0x0017;
        public const int DBT_USERDEFINED = 0xFFFF;
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        
                        case DBT_DEVICEARRIVAL://监测到存储设备的插入
                            UpanManager.refresh();
                            if (UpanManager.showable) UpanManager.show();
                            break;
                        case DBT_DEVICEREMOVECOMPLETE://检测到存储设备的卸载
                            UpanManager.refresh();
                            if (UpanManager.showable) UpanManager.show();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            base.WndProc(ref m);
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置窗口无边界，使用自定义的标题栏
            this.FormBorderStyle = FormBorderStyle.None;
            this.Left = this.Top = 0;
            int formWidth = this.Width;
            int formHeight = this.Height;
            int screenWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            int screenHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
            int top = (screenHeight - formHeight) / 2;
            int left= (screenWidth - formWidth) / 2;
            this.Top = top;
            this.Left = left;

            //设置界面
            pictureBox1.Image = imageList3.Images[0];
            pictureBox2.Image = imageList3.Images[2];
            pictureBox3.Image = imageList3.Images[6];
            pictureBox5.Image = imageList3.Images[8];


            //注册管理类
            UpanManager.panel2 = panel2;
            UpanManager.panel3 = panel3;
            UpanManager.label2 = label2;
            UpanManager.label5 = label5;
            UpanManager.pictureBox6 = pictureBox6;

            //刷新界面
            UpanManager.refresh();
            UpanManager.show();



        }
        

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            // this.Visible = false;                         //隐藏
            this.Close();
        }
       
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            int screenWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width ;
            int screenHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height ;

            if (this.WindowState == FormWindowState.Maximized)//当前是最大化状态
            {
                this.WindowState = FormWindowState.Normal;      //正常
                pictureBox2.Image = imageList3.Images[2];

                //panel1修改
                int offset = 884 - screenWidth;//偏移量
                panel1.Width += offset;
                pictureBox1.Left += offset;
                pictureBox2.Left += offset;
                pictureBox3.Left += offset;

                //panel2修改
                int panel2_left = (panel2.Parent.Width - panel2.Width) / 2;
                panel2.Left = panel2_left;
                panel2.Height -= 100;

                //panel3修改
                panel3.Height -= 100;

                //panel4修改
                panel4.Top = this.Height - panel4.Height+4;
                panel4.Width = this.Width;
                label4.Left = (label4.Parent.Width - label4.Width) / 2;

                //label3修改
                label3.Top -= 50;

                //pictureBox5修改
                pictureBox5.Top -= 50;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;   //最大化
                pictureBox2.Image = imageList3.Images[4];

                //panel1修改
                int offset = screenWidth - panel1.Width;//偏移量
                panel1.Width += offset;
                pictureBox1.Left += offset;
                pictureBox2.Left += offset;
                pictureBox3.Left += offset;

                //panel2修改
                int panel2_left = (panel2.Parent.Width - panel2.Width) / 2;
                panel2.Left = panel2_left;
                panel2.Height += 100;//需要扩展panel2的长度，否则panel3扩展的长度显示不出来。

                //panel3修改
                panel3.Height += 100;

                //panel4修改
                panel4.Top = this.Height - panel4.Height+4;
                panel4.Width = this.Width;
                label4.Left = (label4.Parent.Width - label4.Width) / 2;

                //label3修改
                label3.Top += 50;

                //pictureBox5修改
                pictureBox5.Top += 50;
            }           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;   //最小化
        }

        //panel1的移动带着窗体的移动。
        bool formOn = false;//一开始没有点击上
        int beginX, beginY;

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            beginX = e.Location.X;
            beginY = e.Location.Y;
            formOn = true;
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            formOn = false;
        }



        private void label3_Click_1(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            UpanManager.refresh();
            UpanManager.show();
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_DoubleClick(object sender, EventArgs e)
        {
            pictureBox2_Click(sender, e);
        }

        bool on = true;
        private void pictureBox6_Click(object sender, EventArgs e)
        {
            if (on)
            {
                //逻辑上的修改
                RegistryKey key = Registry.LocalMachine;
                RegistryKey software = key.OpenSubKey(@"SYSTEM\CurrentControlSet\services\USBSTOR", true);
                
                try
                {
                    software.SetValue("Start", 4);
                }
                catch(Exception)
                {

                    MessageBox.Show("您需要允许软件的修改\n才可以禁用U盘哟", "失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
                finally
                {
                    //物理上的修改
                    if (software.GetValue("Start").ToString() == "4")
                    {
                        pictureBox6.Image = imageList2.Images[1];
                        on = false;
                        MessageBox.Show("您已经成功禁用了U盘的识别", "成功",MessageBoxButtons.OK,MessageBoxIcon.Information);

                    }
                }
            }
            else
            {
                //逻辑上的修改
                RegistryKey key = Registry.LocalMachine;
                RegistryKey software = key.OpenSubKey(@"SYSTEM\CurrentControlSet\services\USBSTOR", true);

                try
                {
                    software.SetValue("Start", 2);
                }
                catch (Exception)
                {

                    MessageBox.Show("您需要允许软件的修改\n才可以开启U盘哟", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    //物理上的修改
                    if (software.GetValue("Start").ToString() == "2")
                    {
                        pictureBox6.Image = imageList2.Images[0];
                        on = true;
                        MessageBox.Show("您已经成功开启了U盘的识别", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList3.Images[1];
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList3.Images[0];
        }

        private void pictureBox3_MouseEnter(object sender, EventArgs e)
        {
            pictureBox3.Image = imageList3.Images[7];
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            try
            {
                pictureBox3.Image = imageList3.Images[6];
            }catch(Exception)
            {
                //只会在最后关闭的时候出错
            }
            
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)//当前是最大化状态
            {
                pictureBox2.Image = imageList3.Images[5];
            }
            else
            {
                pictureBox2.Image = imageList3.Images[3];
            }

        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)//当前是最大化状态
            {
                pictureBox2.Image = imageList3.Images[4];
            }
            else
            {
                pictureBox2.Image = imageList3.Images[2];
            }
        }

        private void pictureBox5_MouseEnter(object sender, EventArgs e)
        {
            pictureBox5.Image = imageList3.Images[9];
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            pictureBox5.Image = imageList3.Images[8];
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (formOn)
            {
                int Xto = e.Location.X;
                int Yto = e.Location.Y;
                this.Left += Xto - beginX;
                this.Top += Yto - beginY;
            }
        }
    }
}
